package com.nttuan.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nttuan.flows.SysContactListener;
import com.nttuan.model.entity.Contact;
import com.nttuan.model.entity.User;
import com.nttuan.mycalender.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by TuanNT on 11/6/2014.
 */
public class SysContactAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<User> list;
    private List<Contact> currentList;

    private Context context;
    private SysContactListener listener;
    private int oldPostion;

    public SysContactAdapter(Context context, List<User> list, List<Contact> listContact) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        this.currentList = listContact;
    }

    public void setListener(SysContactListener listener) {
        this.listener = listener;
    }

    public List<User> getList() {
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    public void setCurrentList(List<Contact> currentList) {
        this.currentList = currentList;
    }

    @Override
    public int getCount() {
        if(list == null)
            return  0;
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHoller holler;
        if (view == null) {
            holler = new ViewHoller();
            view = inflater.inflate(R.layout.item_sys_contact, null);
            holler.status = (ImageView) view.findViewById(R.id.imageViewSysContactStatus);
            holler.name = (TextView) view.findViewById(R.id.textSysContactItemName);
            holler.email = (TextView) view.findViewById(R.id.textSysContactItemEmail);
            view.setTag(holler);
        } else
            holler = (ViewHoller) view.getTag();
        User user = list.get(i);
        holler.name.setText(user.getUserName());
        holler.status.setVisibility(View.GONE);
        if(user.getEmail() != null)
            holler.email.setText( context.getString(R.string.sys_contact_email, user.getEmail()));
        if (currentList != null) {
            for (Contact item : currentList) {
                if (item.getUser().getEmail().equals(user.getEmail())) {
                    holler.status.setVisibility(View.VISIBLE);
                    break;
                }
            }
        }
        return view;
    }

    class ViewHoller {
        ImageView status;
        TextView name;
        TextView email;

    }
}
